package com.example.Lezedata.ui.fragment.commonfragment;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.util.GBInterface;

public class inputTextField extends Fragment {
    public interface RightCustomViewCreateListener{
        void createView(TextView textView);
    }

    /// 区号码选择
    public TextView textView_code;
    /// 输入框
    public EditText textView_input;
    /// 右边可自定义view
    public TextView rightView;
    public RightCustomViewCreateListener customViewCreateListener;

    public InputTextFieldViewModel mViewModel;
    public GBInterface viewCreatedListener;
    public static inputTextField newInstance() {
        return new inputTextField();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mViewModel = new InputTextFieldViewModel();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.input_textfield, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        textView_code = view.findViewById(R.id.textView_code);
        textView_input = view.findViewById(R.id.textView_input);
        rightView = view.findViewById(R.id.right_view);
        if (customViewCreateListener != null){
            customViewCreateListener.createView(rightView);
        }
        mViewModel.code.observe(getViewLifecycleOwner(), new Observer<CharSequence>() {
            @Override
            public void onChanged(CharSequence charSequence) {
                textView_code.setText(charSequence);
            }
        });

        mViewModel.hint.observe(getViewLifecycleOwner(), new Observer<CharSequence>() {
            @Override
            public void onChanged(CharSequence charSequence) {
                textView_input.setHint(charSequence);
            }
        });
        mViewModel.inputText.observe(getViewLifecycleOwner(), new Observer<CharSequence>() {
            @Override
            public void onChanged(CharSequence charSequence) {
                textView_input.setText(charSequence);
            }
        });
        if (viewCreatedListener != null){
            viewCreatedListener.onViewCreated(view);
        }
    }

    public CharSequence inputText(){
        return textView_input.getText();
    }

    public CharSequence code(){
        return textView_code.getText();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(InputTextFieldViewModel.class);
        // TODO: Use the ViewModel
    }

}